let storage = bui.storage();
let language,translate;
loader.define(function (require, exports, module, global) {
    let pageview = {
        init() {
            translate = global.translate
            language = storage.get("language", 0)
            if (!language) {
                language = 'chinese_simplified'
            }

            try {
                translate.selectLanguageTag.show = false;
                translate.ignore.id.push('languageContent')
                let curLanguage = translate.language.getCurrent()
                if (curLanguage !== language) {
                    translate.changeLanguage(language)
                }
                translate.execute();
            }catch (e) {
                console.log("切换语言报错")
            }
            if (language === "chinese_simplified") {
                router.$("#insertAdvertise").css("font-size", "0.3rem")
            } else {
                router.$("#insertAdvertise").css("font-size", "0.22rem")
            }
            let languageArr = storage.get('languageArr',0)
            let html = ''
            let html2 = '<select class="languageSelect" id="languageSelectId">\n'
            if (language !== 'chinese_simplified' && language !== 'english') {
                languageArr.forEach(lan => {
                    if (lan.id === language) {
                        html += '<span class="language" id="'+lan.id+'">'+lan.name+'</span>|'
                    } else {
                        html2 += '<option value="'+lan.id+'">'+lan.name+'</option>'
                    }
                })
            } else {
                languageArr.forEach(lan => {
                    html2 += '<option value="'+lan.id+'">'+lan.name+'</option>'
                })
            }
            html+= '<span class="language" id="english">English</span>|<span class="language" id="chinese_simplified">简体中文</span>'
            router.$("#languageContent").html(html + html2 + "</select>")

            router.$(".language").click(function () {
                let selectId = $(this).attr('id')
                if (selectId !== language) {
                    storage.set("language", selectId)
                    location.reload()
                }
            })
            router.$("#languageSelectId").val("");
            router.$("#languageSelectId").change(function () {
                let selectId = $(this).val()
                if (selectId !== language) {
                    storage.set("language", selectId)
                    location.reload()
                }
            })

            let uiLoading = bui.loading({
                appendTo:"#advertiseListLoading",
                width: 40,
                height: 40,
                text:"加载中。。。",
                opacity: 0.5,
                autoClose: false,
                callback: function (argument) {
                }
            });

            let token = storage.get("token",0)
            let user = storage.get("user",0)
            let id;

            router.$("#advertiseFh").click(function () {
                storage.set("tzPage", '0')
                router.replace({ url: "pages/mainIndex/mainIndex.html"});
            })

            router.$("#insertAdvertise").click(function () {
                //todo 跳转广告添加
                router.load({ url: "pages/advertiseEdit/advertiseEdit.html"});
            })

            // if ((user.adminType && user.adminType == "0") || user.userId == 1) {
                router.$("#insertAdvertise").show()
            // }

            if (token) {
                // uiLoading.show()
                bui.ajax({
                    url: global.api.verifyToken,
                    method: "POST",
                    headers: {Token: token}
                }).then(function(res){
                    if (res.code != '200') {
                        uiLoading.stop()
                        bui.confirm({
                            "title": "",
                            "height": 460,
                            "content": '<div class="bui-box-center"><div><h3>登录已过期</h3></div></div>',
                            "buttons": [{ name: "确定", className: "primary-reverse" }],
                            callback:function(){
                                this.close();
                                storage.set("dlTz", '16')
                                router.replace({ url: "pages/login/login.html", param:{tzSign: '0'}});
                            }
                        });
                    }
                })

                let uiList = bui.list({
                    id: "#advertiseScrollList",
                    url: global.api.getGzhAdvertiseList,
                    pageSize: 10, // 当pageSize 小于返回的数据大小的时候,则认为是最后一页,接口返回的数据最好能返回空数组,而不是null
                    data: {},
                    headers: {Token: token},
                    //如果分页的字段名不一样,通过field重新定义
                    field: {
                        page: "page",
                        size: "limit",
                        data: "data"
                    },
                    callback: function(e) {
                        // e.target 为你当前点击的元素
                        // e.currentTarget 为你当前点击的handle 整行
                        let cla = router.$(e.target).attr("class")
                        id = router.$(e.target).attr("name")
                        let place = router.$(e.target).attr("place")
                        let city = router.$(e.target).attr("city")

                        if (cla.indexOf("edit") > -1) {
                            //编辑
                            router.load({ url: "pages/advertiseEdit/advertiseEdit.html", param: {id: id }});
                        } else if (cla.indexOf("delete") > -1) {
                            //删除
                            bui.confirm("确认要删除此广告么？", function(e) {
                                //this 是指点击的按钮
                                let text = $(e.target).text();

                                if (text == "确定") {
                                    uiLoading.show()
                                    bui.ajax({
                                        url: global.api.deleteAdvertise,
                                        data:{id: id, city: city, place: place},
                                        method: "POST"
                                    }).then(function(res){
                                        if (res.code == '200') {
                                            uiList.refresh();
                                            bui.confirm({
                                                "title": "",
                                                "height": 460,
                                                "content": '<div class="bui-box-center"><div><i class="icon-successfill success"></i><h3>删除成功</h3></div></div>',
                                                "buttons": [{ name: "我知道了", className: "primary-reverse" }]
                                            });
                                        }
                                        uiLoading.stop()
                                    })
                                }
                                this.close()
                            });
                        }
                    },
                    template: function(data) {
                        let fontSize = '0.3rem'
                        let fontSize2 = '0.24rem'
                        if (language !== "chinese_simplified") {
                            fontSize = '0.22rem'
                            fontSize2 = '0.18rem'
                        }

                        var html = "";
                        data.forEach(function(el, index) {
                            let title = el.title ? el.title : ""
                            let articleStr = el.articleStr ? el.articleStr : ""

                            let placeStr = ''
                            if (el.place) {
                                if (el.place.indexOf("1") > -1) {
                                    placeStr += '首页，'
                                }
                                if (el.place.indexOf("2") > -1) {
                                    placeStr += '分类页，'
                                }
                                if (el.place.indexOf("3") > -1) {
                                    placeStr += '详情页，'
                                }
                                if (placeStr) {
                                    placeStr = placeStr.substring(0, placeStr.length - 1)
                                }
                            }

                            html += '<li class="bui-btn">\n' +
                                '<div class="bui-box">'+
                                '                <div class="bui-thumbnail" style="height: 1rem;margin-top: 0.4rem"><img src="'+el.imgUrl+'" alt=""></div>\n' +
                                '                <div class="span1">\n' +
                                '                    <h3 class="item-title" style="font-size: '+fontSize+'">'+title+'</h3>\n'+
                                '                    <p class="item-text" style="font-size: '+fontSize2+'">展示位置：'+placeStr+'</p>\n'+
                                '                    <p class="item-text" style="font-size: '+fontSize2+'">跳转文章：'+articleStr+'</p>\n'+
                                '                    <p class="item-text" style="font-size: '+fontSize2+'">'+el.validDateStr+'</p>\n'+
                                '</div>'+
                                '</div>'

                            if ((user.adminType && user.adminType == "0") || user.userId == 1 || el.user == user.userId) {
                                html += '                            <div class="bui-box">\n' +
                                    '                                <div class="span1">\n' +
                                    '                                    <div class="czbtn edit" style="background: #00B066;font-size: '+fontSize2+'" name="'+el.id+'">\n' +
                                    '                                        编辑\n' +
                                    '                                    </div>\n' +
                                    '                                </div>\n' +
                                    '                                <div class="span1">\n' +
                                    '                                    <div class="czbtn delete" style="background: red;font-size: '+fontSize2+'" name="'+el.id+'" city="'+el.city+'" place="'+el.place+'">\n' +
                                    '                                        删除\n' +
                                    '                                    </div>\n' +
                                    '                                </div>\n'+
                                    '                        </div>\n'
                            }

                            html += '            </li>'
                        });
                        uiLoading.stop()

                        setTimeout(function () {
                            try {
                                translate.execute();
                            }catch (e) {
                                console.log("切换语言报错")
                            }
                        },1000)
                        return html;
                    }
                })
            } else {
                bui.confirm({
                    "title": "",
                    "height": 460,
                    "content": '<div class="bui-box-center"><div><h3>当前未登录，请登录</h3></div></div>',
                    "buttons": [{ name: "登录", className: "primary-reverse" }],
                    callback:function(){
                        this.close();
                        storage.set("dlTz", '16')
                        router.replace({ url: "pages/login/login.html", param:{tzSign: '0'}});
                    }
                });
            }
            try {
                translate.execute();
            }catch (e) {
                console.log("切换语言报错")
            }
        }
    };

    // 初始化
    pageview.init();

    // 输出模块
    return pageview;
})
